﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookManage.BLL;
using BookManage.Model;
using System.Data.SqlClient;
using System.IO;

namespace BookManage
{
    public partial class frmAddBook : Form
    {
        private Book book = new Book();
        private BookAdmin bookBLL = new BookAdmin();
        public frmAddBook()
        {
            InitializeComponent();
        }
        #region 类型转换
        public void SetTextToBook()
        {
            book.bkID = Convert.ToInt32(txtbkID.Text);
            book.bkCode = txtbkCode.Text;
            book.bkName = txtbkName.Text;
            book.bkAuthor = txtbkAuthor.Text;
            book.bkPress = txtbkPress.Text;
            book.bkDatePress = Convert.ToDateTime(txtbkDatePress.Text);
            book.bkISBN = txtbkISBN.Text;
            book.bkCatalog = txtbkCatalog.Text;
            book.bkLanguage = txtbkLanguage.Text;
            book.bkPages = Convert.ToInt32(txtbkPages.Text);
            book.bkPrice = Convert.ToSingle(txtbkPrice.Text);
            book.bkDateIn = Convert.ToDateTime(txtbkDateIn.Text);
           // book.bkNum = Convert.ToInt32(txtbkNum.Text);
            book.bkBrief = txtbkBrief.Text;
            if (BKPhoto.Image != null)
            {
                MemoryStream ms = new MemoryStream();
                BKPhoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                book.bkCover = ms.GetBuffer();
            }
        }

        private void SetBookToText()
        {
            txtbkID.Text = Convert.ToString(book.bkID);
            txtbkCode.Text = book.bkCode;
            txtbkName.Text = book.bkName;
            txtbkAuthor.Text = book.bkAuthor;
            txtbkPress.Text = book.bkPress; ;
            txtbkDatePress.Text = Convert.ToString(book.bkDatePress);
            txtbkISBN.Text = book.bkISBN;
            txtbkCatalog.Text = book.bkCatalog;
            txtbkLanguage.Text = Convert.ToString(book.bkLanguage);
            txtbkPages.Text = Convert.ToString(book.bkPages);
            txtbkPrice.Text = Convert.ToString(book.bkPrice);
            txtbkDateIn.Text = Convert.ToString(book.bkDateIn);
            //txtbkNum.Text = Convert.ToString(book.bkNum);
            txtbkBrief.Text = book.bkBrief;
            if (book.bkCover == null)
            {
                BKPhoto.Image = null;
            }
            else
            {
                MemoryStream ms = new MemoryStream(book.bkCover);
                Image imgPhoto = Bitmap.FromStream(ms, true);
                BKPhoto.Image = imgPhoto;
            }
            //txtbkStatus.Text = book.bkStatus;
        }
        #endregion

        private void btnAddBook_Click(object sender, EventArgs e)
        {
            if (txtbkCode.Text == "" || txtbkAuthor.Text == "" || txtbkPress.Text == "" || txtbkDatePress.Text == "" || txtbkISBN.Text == ""
                || txtbkCatalog.Text == "" || txtbkLanguage.Text == "" || txtbkPages.Text == "" || txtbkPrice.Text == ""
                || txtbkDateIn.Text == "" || txtbkCount.Text == "" || txtbkBrief.Text == "" || txtbkID.Text == ""|| BKPhoto.Image == null)
            {
                MessageBox.Show("请将入库图书信息填写完整！");
            }
            else if (book.bkCode == txtbkCode.Text || book.bkID == Convert.ToInt32(txtbkID.Text))
            {
                MessageBox.Show("该图书编号已经存在，请重新创建！");
            }
            else
            {
                bookBLL.Add(book);
                MessageBox.Show("图书成功入库！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //Form form=new 
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            Form form = new frmMain();
            form.Show();
            this.Close();
        }

        private void btnLoadPicture_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd1 = new OpenFileDialog();
            ofd1.Filter = "图片文件（*.jpg;*bmp;*.png;*.gif）|*.jpg;*bmp;*.png;*.gif";
            if (ofd1.ShowDialog() == DialogResult.OK)
            {
                Image imgPhoto = Image.FromFile(ofd1.FileName);
                BKPhoto.Image = imgPhoto;
            }
        }
    }
}
